﻿Public Class Homepage

	Dim Popup1 As New ContextMenuStrip
	Dim Popup2 As New ContextMenuStrip

	Sub New()
		InitializeComponent()
		ToolBar.Parent = Nothing
		ToolBar.Renderer = New FirefoxRenderer
		BtnHelp.DropDown = HelpMenu
		MnuNew.Image = MakeImage(My.Resources.personel, My.Resources.add, 16, 16)
		MnuEdit.Image = MnuEdit.Image.Resize(16, 16)
		MnuDelete.Image = My.Resources.delete.Resize(16, 16)
		MnuProperties.Image = My.Resources.props.Resize(16, 16)
		BtnEdit.Image = MnuEdit.Image
		MnuIzin.Image = MnuIzin.Image.Resize(16, 16)
		MnuIzinI.Image = FrmIzin.GetImage("I", 16, 16)
		MnuIzinH.Image = FrmIzin.GetImage("H", 16, 16)
		MnuIzinM.Image = FrmIzin.GetImage("M", 16, 16)
		MnuIzinO.Image = FrmIzin.GetImage("O", 16, 16)
		MnuIzinS.Image = FrmIzin.GetImage("S", 16, 16)
		MnuIzinP.Image = FrmIzin.GetImage("P", 16, 16)
		BtnView.Image = BtnView.Image.Resize(16, 16)
		BtnHelp.Image = BtnHelp.Image.Resize(16, 16)
		BtnZoomin.Image = BtnZoomin.Image.Resize(16, 16)
		BtnZoomout.Image = BtnZoomout.Image.Resize(16, 16)
		MnuZoomin.Image = BtnZoomin.Image
		MnuZoomout.Image = BtnZoomout.Image
		MnuRefresh.Image = MnuRefresh.Image.Resize(16, 16)
		'MnuSaatGun.Image = MakeImage(My.Resources.calendar, My.Resources.saat, 16, 16)
		List.Columns.Add("Personel adı", 200)
		List.Columns.Add("Kimlik", 80, HorizontalAlignment.Center)
		List.Columns.Add("Statü", 100)
		List.Columns.Add("Birim", 200)
		List.Columns.Add("Alt birim", 80)
		List.FullRowSelect = True
		List.LargeImageList = New ImageList
		List.SmallImageList = New ImageList
		FontSize = 1
		Popup1.Items.Add(MnuEdit.Clone)
		Popup1.Items.Add(MnuProperties.Clone)
		Popup1.Items.Add(MnuResetPassword.Clone)
		Popup1.Items.Add("-")
		Popup1.Items.Add(MnuIzinI.Clone)
		Popup1.Items.Add(MnuIzinS.Clone)
		Popup1.Items.Add("-")
		Popup1.Items.Add(MnuNew.Clone)
		Popup1.Items.Add(MnuDelete.Clone)
		Popup2.Items.Add(MnuGrup.Clone)
		Popup2.Items.Add(BtnView.Clone)
		Popup2.Items.Add("-")
		Popup2.Items.Add(MnuNew.Clone)
		List.ContextMenuStrip = Popup1
	End Sub

	Sub Populate(Optional ByVal Kod As Integer = 0)
		Dim Selected As New List(Of String)
		For Each Item As ListViewItem In List.Items
			If Item.Selected Then Selected.Add(Item.Name)
		Next
		List.Items.Clear()
		Dim Liste = Service.Personel.DetayliListe(Kullanici.TBirim)
		Dim Items As New List(Of SourceItem)
		Items.Add(New SourceItem("", ""))
		Dim ListItems As New List(Of ListViewItem)
		For Each Personel In Liste
			Dim Item As New ListViewItem
			Item.Text = Personel.AdiSoyadi
			Item.SubItems.AddRange(New String() {Personel.Kimlik, StatuListesi(Personel.Statu).Ad, Personel.Birim, Personel.AltBirim, Mid(Personel.Adi, 1, 1).ToTitle, Mid(Personel.Soyadi, 1, 1).ToTitle})
			Item.Name = Trim(Personel.Kod)
			Item.ImageKey = If(Personel.Cinsiyet = "", "personel", If(Personel.Cinsiyet = "E", "man", "woman"))
			If Not Personel.HasInfo Then Item.ForeColor = Color.Gray : Item.Font = New Font(List.Font, FontStyle.Italic)
			Items.Add(New SourceItem(Personel.Kod, Personel.AdiSoyadi))
			ListItems.Add(Item)
		Next
		List.Items.AddRange(ListItems.ToArray)
		CType(Parent, FrmMain).CmbFind.LoadItems(Items)
		CType(Parent, FrmMain).CmbFind.SelectedIndex = -1
		If Kod = 0 Then
			For Each Item As ListViewItem In List.Items
				If Selected.Contains(Item.Name) Then Item.Selected = True
			Next
			If List.SelectedItems.Count > 0 Then List.SelectedItems(0).EnsureVisible()
		Else
			List.Items(Trim(Kod)).Selected = True
			List.Items(Trim(Kod)).EnsureVisible()
		End If
		If Kullanici.Yonetici Then
			InfoBox.Reset()
			TalepListesi = Service.Talep.OnayBekleyen(Kullanici.MgrBirim).ToList
			If TalepListesi.Count > 0 Then
				InfoBox.ShowMessage("!", TalepListesi.Count & " adet izin talebi onay beklemektedir.", "Görüntüle")
				InfoBox.Bind(0, AddressOf GotoTalep)
			End If
			MnuIzin.Enabled = False
		ElseIf Kullanici.Operator Then
			InfoBox.Reset()
			TalepListesi = Service.Talep.Onaylanan(Kullanici.OprBirim).ToList
			If TalepListesi.Count > 0 Then
				InfoBox.ShowMessage("!", TalepListesi.Count & " adet onaylanan izin talebi işlenmeyi beklemektedir.", "Görüntüle")
				InfoBox.Bind(0, AddressOf GotoTalep)
			End If
		End If
		If Kullanici.Operator Or Kullanici.Yonetici Then
			Dim Sorunlu = Service.Sorunlu.Birim(Kullanici.TBirim)
			If Sorunlu.Count > 0 Then
				InfoBox.ShowMessage("!", "Hatalı izin kayıtları vardır. Kırmızı ile işaretlenmiş personelin izin kayıtlarını gözden geçirin.", "Çözümle")
				InfoBox.Bind(0, AddressOf HatalariCozumle)
				For Each Item In Sorunlu
					List.Items(Trim(Item)).ForeColor = Color.Red
				Next
			End If
		End If
	End Sub

	Private Sub MnuIzin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuIzin.Click
		If MnuIzin.DropDownButtonPressed Then Exit Sub
		MnuIzinI.PerformClick()
	End Sub

	Private Sub MnuNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuNew.Click
		Dim Form As New FrmKart
		Form.Text = "Yeni Personel Girişi"
		If Not Form.ShowDialog = Windows.Forms.DialogResult.OK Then Exit Sub
		Populate(Form.Kod)
	End Sub

	Private Sub MnuDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuDelete.Click
		If List.SelectedItems.Count <> 1 Then Exit Sub
		If Not Confirm(List.SelectedItems(0).Text & " isimli personelin kaydını silmek istediğinizden emin misiniz?") Then Exit Sub
		Service.Personel.Sil(List.SelectedItems(0).Name, Kullanici.Kod)
		List.SelectedItems(0).Remove()
	End Sub

	Private Sub MnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuExit.Click
		FindForm.Close()
	End Sub

	Private Sub MnuGrup_DropDownItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MnuGrup.DropDownItemClicked
		Dim Groups As New SortedList(Of String, String)
		List.Groups.Clear()
		If e.ClickedItem Is MnuGrupRemove Then Exit Sub
		For i As Integer = 1 To 2
			For Each Item As ListViewItem In List.Items
				Dim Grup As String = ""
				Select Case e.ClickedItem.Name
					Case "MnuGrupAd" : Grup = Item.SubItems(5).Text
					Case "MnuGrupSoyad" : Grup = Item.SubItems(6).Text
					Case "MnuGrupStatu" : Grup = Item.SubItems(2).Text
					Case "MnuGrupBirim" : Grup = Item.SubItems(3).Text
					Case "MnuGrupAltbirim" : Grup = Item.SubItems(4).Text
				End Select
				If i = 1 Then
					If Not Groups.ContainsKey(Grup) Then Groups.Add(Grup, Grup)
				Else
					List.Groups(Grup).Items.Add(Item)
				End If
			Next
			If i = 1 Then
				For Each Grup In Groups.Values
					List.Groups.Add(Grup, Grup)
				Next
			End If
		Next
		List.ShowGroups = True
		If List.View = View.List Then List.View = View.LargeIcon
	End Sub

	Private Sub BtnView_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnView.ButtonClick
		If BtnView.DropDownButtonPressed Then Exit Sub
		List.View = If(List.View = View.LargeIcon, View.List, If(List.View = View.List, View.Details, View.LargeIcon))
	End Sub

	Private Sub BtnView_DropDownItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles BtnView.DropDownItemClicked
		Select Case Strings.Right(e.ClickedItem.Name, 1)
			Case "1" : List.View = View.LargeIcon
			Case "2" : List.View = View.List
			Case "3" : List.View = View.Details
		End Select
	End Sub

	Private Sub MnuResetPassword_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuResetPassword.Click
		If List.SelectedItems.Count <> 1 Then Exit Sub
		If Not Confirm(List.SelectedItems(0).Text & " isimli personelin parolasını sıfırlamak istediğinizden emin misiniz?") Then Exit Sub
		Kullanici.SifreSifirla(List.SelectedItems(0).Name)
		Info("Parola sıfırlanmıştır. İlgili personel sisteme yeniden giriş yaparken yeni parolası sorulacaktır.")
	End Sub

	Private Sub MnuProperties_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuProperties.Click
		If List.SelectedItems.Count <> 1 Then Exit Sub
		If Not CtlPersonel.Edit(List.SelectedItems(0).Name) Then Exit Sub
	End Sub

	Private Sub MnuHelpStatu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
		Dim Text As String = ""
		'For Each Item In Statuler("")
		'    Text &= Item.Adi & " (" & Item.Kod & ") : Ayda " & Item.Izin & " gün izin hakkı kazandırır." & vbCrLf
		'Next
		'Info(Text)
	End Sub

	Private Sub MnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
		Info("Personel İzin Takip Sistemi" & vbCrLf & vbCrLf & "Sosyal Sigortalar Dairesi Bilgi İşlem Bölümü" & vbCrLf & "Ahmet Demirağ ve Kemal Tunçbilek tarafından geliştirilmiştir.")
	End Sub

	Private Sub List_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles List.MouseUp
		List.ContextMenuStrip = If(List.SelectedItems.Count = 1, Popup1, Popup2)
	End Sub

	Dim m_FontSize As Integer = 1
	Property FontSize As Integer
		Get
			Return m_FontSize
		End Get
		Set(ByVal Value As Integer)
			m_FontSize = Value
			Dim Items(List.Items.Count - 1) As ListViewItem
			List.Items.CopyTo(Items, 0)
			Dim View = List.View
			List.Items.Clear()
			List.View = Windows.Forms.View.LargeIcon
			List.View = View
			List.Font = New Font(List.Font.Name, Choose(m_FontSize + 1, 8, 9, 10, 12, 14))
			List.Refresh()
			For Each Item In Items
				Item.Font = New Font(List.Font, Item.Font.Style)
			Next
			BtnZoomin.Enabled = Value < 4
			BtnZoomout.Enabled = Value > 0
			Dim S(1) As Integer
			S(0) = Choose(m_FontSize + 1, 17, 19, 21, 24, 27)
			S(1) = Choose(m_FontSize + 1, 29, 32, 35, 39, 43)
			List.LargeImageList.ImageSize = New Size(S(1), S(1))
			List.SmallImageList.ImageSize = New Size(S(0), S(0))
			List.LargeImageList.Images.Add("personel", My.Resources.personel.Resize(S(1), S(1), 1))
			List.SmallImageList.Images.Add("personel", My.Resources.personel.Resize(S(0), S(0), 1))
			List.LargeImageList.Images.Add("man", My.Resources.man.Resize(S(1), S(1), 1))
			List.SmallImageList.Images.Add("man", My.Resources.man.Resize(S(0), S(0), 1))
			List.LargeImageList.Images.Add("woman", My.Resources.woman.Resize(S(1), S(1), 1))
			List.SmallImageList.Images.Add("woman", My.Resources.woman.Resize(S(0), S(0), 1))
			List.Items.AddRange(Items)
			'List.Columns(0).Width = a
		End Set
	End Property

	Private Sub Zoom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnZoomin.Click, BtnZoomout.Click
		FontSize = FontSize + If(sender Is BtnZoomin Or sender Is MnuZoomin, 1, -1)
	End Sub

	'Private Sub MnuSaatGun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuSaatGun.Click
	'    If Not Confirm("Saatlik izinleri yıllık izinlere çevirmek istediğinizden emin misiniz?") Then Exit Sub
	'    Dim Rs As Recordset = OpenSQL("select personel,sum(case tur when '*' then saat when 'S' then saat when 'G' then -saat end) as saat from hareket group by personel")
	'    Do Until Rs.EOF
	'        Dim Gun As Integer = Rs("saat") \ 8
	'        If Gun > 0 Then ExecSQL("insert into hareket (personel,tarih1,tur,gun,saat) values (:personel,:tarih,'G',:gun,:saat)", Rs("personel"), Today, Gun, Gun * 8)
	'        Rs.MoveNext()
	'    Loop
	'End Sub

	Private Sub MnuRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuRefresh.Click
		Populate()
	End Sub

	Private Sub MnuFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuFind.Click
		CType(Parent, FrmMain).CmbFind.Focus()
	End Sub

	Private Sub MnuIzin_DropDownItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MnuIzin.DropDownItemClicked
		If Not e.ClickedItem.Name Like "MnuIzin*" Then Exit Sub
		Dim Form As New FrmIzin(Strings.Right(e.ClickedItem.Name, 1))
		If List.SelectedItems.Count = 1 Then Form.SetPersonel(List.SelectedItems(0).Name)
		Form.Show()
	End Sub

	Dim TalepListesi As New List(Of TalepBilgisi)
	Dim TalepIndex As Integer = 0

	Sub GotoTalep()
		InfoBox.Reset()
		TalepBox.Reset()
		If TalepIndex >= TalepListesi.Count Then TalepIndex = 0
		If TalepListesi.Count = 0 Then
			TalepBox.ShowMessage("i", "Bekleyen izin talebi kalmadı.", "Kapat")
			TalepBox.Bind(0, AddressOf TalepBox.Reset)
			Exit Sub
		End If
		With TalepListesi(TalepIndex)
			If Kullanici.Yonetici Then
				TalepBox.ShowMessage("i", Service.Personel.Oku(.Personel).AdiSoyadi & ", " & .Tarih1.DMY & " - " & .Tarih2.DMY & " günleri arasında izin talebinde bulundu.", "İzin kartı|Onayla|Reddet" & If(TalepListesi.Count = 1, "", "|Sonraki"))
				TalepBox.Bind(0, AddressOf IzinKartiAc)
				TalepBox.Bind(1, AddressOf IzinTalebiOnayla)
				TalepBox.Bind(2, AddressOf IzinTalebiReddet)
				TalepBox.Bind(3, AddressOf SonrakiIzinTalebi)
			ElseIf Kullanici.Operator Then
				TalepBox.ShowMessage("i", Service.Personel.Oku(.Personel).AdiSoyadi & ", " & .Tarih1.DMY & " - " & .Tarih2.DMY & " günleri arasındaki izin talebi onaylanmıştır.", "İzin kartı|Sisteme işle" & If(TalepListesi.Count = 1, "", "|Sonraki"))
				TalepBox.Bind(0, AddressOf IzinKartiAc)
				TalepBox.Bind(1, AddressOf IzinTalebiIsle)
				TalepBox.Bind(2, AddressOf SonrakiIzinTalebi)
			End If
		End With
	End Sub

	Sub HatalariCozumle()
		Dim Sorunlu = Service.Sorunlu.Birim(Kullanici.TBirim)
		If Sorunlu.Count = 0 Then
			InfoBox.Reset()
			Exit Sub
		End If
		Open(Sorunlu.First)
	End Sub

	Sub IzinKartiAc()
		List.SelectedItems.Clear()
		List.Items(Trim(TalepListesi(TalepIndex).Personel)).Selected = True
		BtnEdit.PerformClick()
	End Sub

	Sub IzinTalebiOnayla()
		If Not Confirm("İzin talebini onaylamak istediğinizden emin misiniz?") Then Exit Sub
		Service.Talep.Onayla(TalepListesi(TalepIndex).Personel, Kullanici.Kod)
		TalepListesi.RemoveAt(TalepIndex)
		GotoTalep()
	End Sub

	Sub IzinTalebiReddet()
		If Not Confirm("İzin talebini reddetmek istediğinizden emin misiniz?") Then Exit Sub
		Service.Talep.Reddet(TalepListesi(TalepIndex).Personel, Kullanici.Kod)
		TalepListesi.RemoveAt(TalepIndex)
		GotoTalep()
	End Sub

	Sub IzinTalebiIsle()
		Service.Talep.Isle(TalepListesi(TalepIndex).Personel, Kullanici.Kod)
		TalepListesi.RemoveAt(TalepIndex)
		GotoTalep()
	End Sub

	Sub SonrakiIzinTalebi()
		TalepIndex += 1
		GotoTalep()
	End Sub

	Private Sub MnuMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuMap.Click
		CType(FindForm(), FrmMain).ShowMap()
	End Sub

	Private Sub MnuReports_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuReports.Click, BtnReports.ButtonClick
		CType(FindForm(), FrmMain).ShowReports()
	End Sub

	Private Sub MnuOwn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuOwn.Click
		Open(Kullanici.Kod)
	End Sub

	Sub Open(ByVal Kod As Integer)
		If Kod = 0 Then Exit Sub
		List.SelectedItems.Clear()
		List.Items(Trim(Kod)).Selected = True
		MnuEdit.PerformClick()
	End Sub

	Private Sub BtnEdit_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.ButtonClick, MnuEdit.Click, List.Activate
		If List.SelectedItems.Count <> 1 Then Exit Sub
		CType(FindForm(), FrmMain).Open(List.SelectedItems(0).Name)
	End Sub

	Private Sub MnuSaatToplam_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuSaatToplam.Click, BtnSaatToplam.Click
		Dim Form As New FrmSaatToplam
		Form.ShowDialog()
	End Sub

	Private Sub BtnEdit_DropDownItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles BtnEdit.DropDownItemClicked
		If e.ClickedItem.Name Like "MnuRecent*" Then
			CType(FindForm(), FrmMain).Open(Mid(e.ClickedItem.Name, 10))
		End If
	End Sub

	Private Sub BtnEdit_DropDownOpening(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEdit.DropDownOpening
		For i = BtnEdit.DropDownItems.Count - 1 To 1 Step -1
			BtnEdit.DropDownItems.RemoveAt(i)
		Next
		BtnEdit.DropDownItems.Add("-")
		For Each Item In Kullanici.SonAcilanlar(Kullanici.Kod)
			Dim Menu = BtnEdit.DropDownItems.Add(Item.Ad)
			Menu.Name = "MnuRecent" & Item.Kod
		Next
	End Sub

End Class
